//
//  JKCellAppIndexActivity.m
//  chequanbao_ios
//
//  Created by 朱凯 on 2018/1/23.
//  Copyright © 2018年 com.itboye. All rights reserved.
//

#import "JKCellAppIndexActivity.h"
@implementation JKCellAppIndexActivity
{
    NSTimer *_timer;
    UIScrollView *_scroll;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor whiteColor];
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    UIImageView *imageView = [UIImageView new];
    imageView.image = [UIImage imageNamed:@"首页-活动"];
    [self.contentView addSubview:imageView];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.text = @"超值活动：";
    titleLabel.font = FONT(15);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor blackColor];
    [self.contentView addSubview:titleLabel];
    
    _scroll = [UIScrollView new];
    _scroll.scrollEnabled = false;
    _scroll.delegate = self;
    [self.contentView addSubview:_scroll];
    

    
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.top.equalTo(@20);
        make.width.height.equalTo(@20);
    }];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageView.mas_right).offset(10);
        make.top.equalTo(@10);
        make.height.equalTo(@40);
    }];
    [_scroll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel.mas_right);
        make.top.equalTo(titleLabel);
        make.right.equalTo(@-10);
        make.height.equalTo(@40);
        // 设置cell高度
        make.bottom.equalTo(@-10);
    }];
    
    
}
- (void)setActivitys:(NSArray *)activitys {
    
    if (_timer) {
        [_timer invalidate];
        _timer = nil;
    }
    
    NSLog(@"zk...%@", activitys);
    
    _activitys = activitys;
    __block UILabel *last = nil;
    [_scroll.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    for (int i = 0; i < activitys.count + 1; i ++) {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, last ? last.bottom : 0, _scroll.width, _scroll.height)];
        label.numberOfLines = 2;
        if (i == activitys.count) {
            label.text = activitys.firstObject;
        } else {
            label.text = activitys[i];
        }
        label.font = FONT(15);
        label.textAlignment = NSTextAlignmentLeft;
        label.textColor = [UIColor colorWithRGBHex:0x999999];
        [_scroll addSubview:label];
        
        last = label;
    }


    _scroll.contentSize = CGSizeMake(0, _scroll.subviews.lastObject.bottom);
    _scroll.contentOffset = CGPointMake(0, _scroll.height);
    _timer = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    
}
- (void)timerAction {

    [UIView animateWithDuration:0.5 animations:^{
        _scroll.contentOffset = CGPointMake(0, _scroll.contentOffset.y + _scroll.height);
    } completion:^(BOOL finished) {
        if (_scroll.contentOffset.y == (_scroll.contentSize.height - _scroll.height)) {
            _scroll.contentOffset = CGPointMake(0, 0);
        }
    }];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    
}


@end
